<?php
namespace app\services\ai;

use app\services\ai\traits\HasHttpRequest;
use app\services\ai\traits\youtu\HumanRecognitionTrait;
use app\services\ai\traits\youtu\ImageProcessorTrait;

class Youtu implements AiInterface
{
    use HasHttpRequest, HumanRecognitionTrait, ImageProcessorTrait;

    const
        API_YOUTU_END_POINT = 'https://api.youtu.qq.com/',
        API_YOUTU_CHARGE_END_POINT = 'https://vip-api.youtu.qq.com/',
        API_TENCENTYUN_END_POINT = 'https://youtu.api.qcloud.com/',

        EXPIRED_SECONDS = 2592000, //过期时间为30天
        MODEL_ALL_FACE = 0, //检查所有人脸
        MODEL_SINGLE_FACE = 1, //检查照片脸最大的，适合单人照
        RSP_IMG_TYPE_URL = 'url',
        RSP_IMG_TYPE_BASE64 = 'base64',

        //人脸识别API
        API_DETECT_FACE = 'youtu/api/detectface',
        API_DETECT_FACE_SHAPE = 'youtu/api/faceshape',
        API_FACE_COMPARE = 'youtu/api/facecompare',
        API_FACE_VERIFY = 'youtu/api/faceverify',
        API_FACE_IDENTIFY = 'youtu/api/faceidentify',
        API_MULTI_FACE_IDENTIFY = 'youtu/api/multifaceidentify',
        API_NEW_PERSON = 'youtu/api/newperson',
        API_DEL_PERSON = 'youtu/api/delperson',
        API_ADD_FACE = 'youtu/api/addface',
        API_DEL_FACE = 'youtu/api/delface',
        API_SET_INFO = 'youtu/api/setinfo',
        API_GET_INFO = 'youtu/api/getinfo',
        API_GET_GROUP_IDS = 'youtu/api/getgroupids',
        API_GET_PERSON_IDS = 'youtu/api/getpersonids',
        API_GET_FACE_IDS = 'youtu/api/getfaceids',
        API_GET_FACE_INFO = 'youtu/api/getfaceinfo',

        //音频API
        TEXT_MAX_LENGTH = 300, //语音文本长度
        API_TEXT_TO_AUDIO = 'youtu/ttsapi/text_to_audio',

        API_CAR_CLASSIFY = 'youtu/carapi/carclassify',

        //人体识别
        API_HAND_TRACK = 'youtu/handtracking/classify',
        //人脸融合
        API_MERGE_FACE = 'cgi-bin/pitu_open_access_for_youtu.fcg';

    protected $config;

    protected $timeout = 15.0;

    public function __construct($config)
    {
        $this->config = $config;
    }

    /**
     * 返回签名字符串
     * @param $config
     * @return bool|string
     */
    public function sign($config)
    {
        $baseParams = [
            'u' => array_get($config, 'user_id'),
            'a' => array_get($config, 'app_id'),
            'k' => array_get($config, 'secret_id'),
            'e' => time() + self::EXPIRED_SECONDS,
            't' => time(),
            'r' => rand(),
            'f' => '',
        ];
        $plainText = http_build_query($baseParams);
        return base64_encode(hash_hmac("SHA1", $plainText, array_get($config, 'secret_key'), true) . $plainText);
    }

    public function getBaseUri()
    {
        return self::API_YOUTU_END_POINT;
    }

    public function parseOptions($params)
    {
        return [
            'json' => array_merge($params, ['app_id' => array_get($this->config, 'app_id')]),
            'headers' => [
                'Authorization' => $this->sign($this->config),
                'Content-Type' => 'text/json',
            ]
        ];
    }
}
